package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.mapper.SupplierMapper;
import com.ktjiaoyu.service.SupplierService;
import com.ktjiaoyu.utils.Page;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @return 13141
 * 2025/6/25
 * 08:46
 */
@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierServiceImpl.class);
    @Resource
    private SupplierMapper supplierMapper;
    // 提供setter方法注入属性
    public void setSupplierMapper(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }
    @Override
    public List<Supplier> getSupplierList() {
        return supplierMapper.getSupplierList();
    }

    @Override
    public List<Supplier> getSupplierListByName(String supName) {
        return supplierMapper.getSupplierListByName(supName);
    }

    @Override
    @Transactional
    public int saveSupplier(List<Supplier> supplierList) {
        int rows = 0;
        for (int i = 0; i < supplierList.size(); i++){
            // 拿出每一条用户数据插入
            Supplier supplier = supplierList.get(i);
            if (i == 1){
                // 插入第二条数据时，模拟数据库异常，查看事务是否生效
                throw new RuntimeException("数据库异常");
            }
            // 调用插入数据操作，计入总受影响操作
            rows += supplierMapper.addSupplier(supplier);
        }
        // 返回总受影响操作
        return rows;
    }

    @Override
    @Transactional
    public int deleteSupplier(Integer id) {
        return supplierMapper.deleteSupplier(id);
    }

    @Override
    @Transactional
    public int updateSupplier(Supplier supplier) {
        return supplierMapper.updateSupplier(supplier);
    }

    // 多条件查询供货商分页数据
    @Override
    public Page<Supplier> findPage(Supplier entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<Supplier> page = null;
        try {
            page = new Page<Supplier>();
            page.setPageSize(pageSize);
//            查询总数，计算总页数
            Long count = supplierMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);
//            查询分页数据
            List<Supplier> data = supplierMapper.getPage(entity, page.offset(), page.getPageSize());
            page.setData(data);
        } catch (Exception e) {
            LOGGER.error("执行 SupplierServiceImpl.findPage(SysUser,Integer, Integer)方法出现异常.", e);
            throw e;
        }
        return page;
    }
    //    保存或修改
    @Override
    public boolean saveOrUPdate(Supplier entity) throws Exception {
        try {
            if (entity.getId() == null) {
//                无主题则新增
                return supplierMapper.addSupplier(entity) > 0;
            } else {
//                有主题则修改
                return supplierMapper.updateSupplier(entity) > 0;
            }
        } catch (Exception e) {
            LOGGER.error("执行 SupplierServiceImpl.saveOrUpdate(Supplier)方法出现异常，", e);
            throw e;
        }
    }
    //    根据 id 查询
    @Override
    public Supplier findById(Long id) throws Exception {
        try {
            return supplierMapper.getById(id);
        } catch (Exception e) {
            LOGGER.error("执行 SupplierServiceImpl" + ".findById(Long)方法出现异常.", e);
            throw e;
        }
    }
}
